package com.fauks.se.lombok;

import lombok.*;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;

//lombok bean相关注解
@ToString(includeFieldNames = true) //生成toString方法
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Data //相当于get、set、hashCode、equals
//@Value //将字段设置为final只有get方法
public class LombokTest {

    //制定get、set范围
    @Setter(AccessLevel.PUBLIC)
    @Getter(AccessLevel.PROTECTED)
    private int id;

    public static void main(String[] args) {
        //testVal();

        //测试notnull
        String name = null;
        //testNotNull(name);

        //使用cleanup解决try catch finally
        //testCleanUp();

        testGetSet();
    }

    //getter/setter注解
    private static void testGetSet() {
        LombokTest lombokTest = new LombokTest();
        lombokTest.setId(12);
        System.out.println(lombokTest);

    }

    private static void testCleanUp() {
        try {
            @Cleanup BufferedReader fr = new BufferedReader(new FileReader("D://a.txt"));
            String line = null;
            while ((line = fr.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //测试NonNull注解
    private static void testNotNull(@NonNull String name) {
        System.out.println("name长度：" + name.length());
        if (name != null) {
            System.out.println(name.length());
        } else {
            throw new NullPointerException("name");
        }
    }

    //使用val声明变量，相当于在变量钱添加final
    private static void testVal() {
        val map = new HashMap<String, String>();
        map.put("name", "fauks");

        //相当于
        final Map<String, String> map2 = new HashMap<>();
    }
}
